package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class UserServiceImpl implements UserService {
    @Override
    public PageInfo<User> findByPage(int currentPage, int pageSize) {
        PageInfo<User> info=null;
        SqlSession sqlSession = null;
        try {
            //获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //执行操作
            //设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            //查询所有
            List<User> list = userDao.findAll();
            //封装分页结果
            info = new PageInfo<>(list);


        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return info;
    }

    /**
     * 保存信息
     * @param user
     */
    @Override
    public void save(User user) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2.获取dao对象的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //给密码加密
            String password = MD5Util.md5(user.getPassword());
            user.setPassword(password);
            //生成唯一ID
            String uuid = UUID.randomUUID().toString().replace("-", "");
            user.setId(uuid);
            //3.执行操作
            userDao.save(user);
            //4.提交事物
            MybatisUtil.commit(sqlSession);
        } finally {
            //5.释放资源
            MybatisUtil.close(sqlSession);
        }

    }

    /**
     * 根据id查询信息
     * @param id
     * @return
     */
    @Override
    public User findById(String id) {
        User user=null;
        SqlSession sqlSession = null;
        try {
            //获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //执行操作
            //查询所有
            user = userDao.findById(id);
            //返回结果
            MybatisUtil.commit(sqlSession);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }

        return user;
    }

    /**
     * 编辑
     * @param user
     */
    @Override
    public void update(User user) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2.获取dao对象的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //3.执行操作
            userDao.update(user);
            //4.提交事物
            MybatisUtil.commit(sqlSession);
        } finally {
            //5.释放资源
            MybatisUtil.close(sqlSession);
        }

    }

    /**
     * 删除
     * @param ids
     */
    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2.获取dao对象的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //3.执行操作
            Stream.of(ids).forEach(id-> {
                //删除用户和角色之间的对应关系1
                userDao.deleteRoleAssociationByUserId(id);
                userDao.delete(id);
            });

            //4.提交事物
            MybatisUtil.commit(sqlSession);
        } finally {
            //5.释放资源
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public List<User> findAll() {
        List<User> list=null;
        SqlSession sqlSession = null;
        try {
            //获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //执行操作
            //查询所有
            list = userDao.findAll();
            //返回结果
            MybatisUtil.commit(sqlSession);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return list;
    }
}
